"""
    This module responds for preferences dialog
"""
import pygtk
import gtk
import gobject

from .. import exceptions
from .. import path
from .. import settings
from .. import plugin
from .. import html

parent = None

def initialize(parent_window):
    """
    Initialize module, parent_window must point to main application window
    """
    global parent

    if not parent is None:
        raise exceptions.InitializationError("Module already initialized!")
    
    parent = parent_window

    
def assert_initialized():
    if parent is None:
        raise exceptions.GuiException("Module 'preferences' is not initialized!")

def run_dialog():
    """
        Display preferences dialog
    """
    assert_initialized()
    xml = gtk.glade.XML(path.get_data_path("pybookshelf.glade"), root="preferences_dialog",
                        domain="pybookshelf")
    dialog = xml.get_widget("preferences_dialog")
    
    dialog.set_transient_for(parent)
    
    # set values
    
    max_cover_width = settings.get_int("ui/cover_max_width_html")
    xml.get_widget("cover_image_max_width").set_value(max_cover_width)
    
    open_new_tab_after_current_one = settings.get_boolean("ui/open_new_tab_after_current_one")
    xml.get_widget("open_new_tab_after_current_checkbox").set_active(open_new_tab_after_current_one) 
    
    ellipsis_in_center = settings.get_boolean("ui/ellipsis_in_center")
    xml.get_widget("ellipsis_in_center_checkbox").set_active(ellipsis_in_center)
    
    open_report_after_update = settings.get_boolean("ui/open_report_after_update")
    xml.get_widget("open_report_after_update_checkbox").set_active(open_report_after_update)
    
    display_rating_on_search_results = settings.get_boolean("ui/display_rating_on_search_results")
    xml.get_widget("display_rating_on_search_results_checkbox").set_active(display_rating_on_search_results)
    
    rename_exported_books = settings.get_boolean("ui/rename_exported_books")
    xml.get_widget("rename_exported_books_checkbox").set_active(rename_exported_books)
    
    report_viewer = settings.get_str("ui/report_viewer")
    xml.get_widget("report_viewer_entry").set_text(report_viewer)
    
    xml.get_widget("open_report_after_update_checkbox")\
        .connect("clicked", on_open_report_clicked, xml.get_widget("report_viewer_entry"))

    normal_fontbutton = xml.get_widget("normal_fontbutton")
    headers_fontbutton = xml.get_widget("headers_fontbutton")
    background_colorbutton = xml.get_widget("background_colorbutton")
    hyperlinks_colorbutton = xml.get_widget("hyperlinks_colorbutton")
    hover_links_colorbutton = xml.get_widget("hover_links_colorbutton")
    normal_text_colorbutton = xml.get_widget("normal_text_colorbutton")
    
    normal_fontbutton.set_font_name(settings.get_str("style/normal_font"))
    headers_fontbutton.set_font_name(settings.get_str("style/headers_font"))
    background_colorbutton.set_color(settings.get_gdk_color("style/background_color"))
    hyperlinks_colorbutton.set_color(settings.get_gdk_color("style/hyperlinks_color"))
    hover_links_colorbutton.set_color(settings.get_gdk_color("style/hover_links_color"))
    normal_text_colorbutton.set_color(settings.get_gdk_color("style/normal_text_color"))

    on_open_report_clicked(xml.get_widget("open_report_after_update_checkbox"), xml.get_widget("report_viewer_entry"))

    notebook = xml.get_widget("preferences_notebook")
    # load tabs for plugins
    plugin_xmls = dict()
    for k, p in plugin.plugins.iteritems():
        # check is plugin provides preferences window
        plugin_glade_xml = p.preferences_glade_root()
        if plugin_glade_xml is None:
            continue
        plugin_xmls[k] = plugin_glade_xml
        p.preferences_init_widgets_values(plugin_xmls[k])
        notebook.append_page(plugin_xmls[k].get_widget("preferences_widget"), tab_label=gtk.Label(_("Plugin %s")%k))
        
    
    response = dialog.run()
    
    # remember entered values
    for k, p in plugin.plugins.iteritems():
        if not k in plugin_xmls:
            continue
        p.preferences_remember_widget_values(plugin_xmls[k])
    
    max_cover_width = xml.get_widget("cover_image_max_width").get_value_as_int()
    settings.set_int("ui/cover_max_width_html", max_cover_width)
    
    open_new_tab_after_current_one = xml.get_widget("open_new_tab_after_current_checkbox").get_active()
    settings.set_boolean("ui/open_new_tab_after_current_one", open_new_tab_after_current_one)
    
    ellipsis_in_center = xml.get_widget("ellipsis_in_center_checkbox").get_active()
    settings.set_boolean("ui/ellipsis_in_center", ellipsis_in_center)
    
    open_report_after_update = xml.get_widget("open_report_after_update_checkbox").get_active()
    settings.set_boolean("ui/open_report_after_update", open_report_after_update)
    
    display_rating_on_search_results = xml.get_widget("display_rating_on_search_results_checkbox").get_active()
    settings.set_boolean("ui/display_rating_on_search_results", display_rating_on_search_results)
    
    rename_exported_books = xml.get_widget("rename_exported_books_checkbox").get_active()
    settings.set_boolean("ui/rename_exported_books", rename_exported_books)
    
    report_viewer = xml.get_widget("report_viewer_entry").get_text()
    settings.set_str("ui/report_viewer", report_viewer)
    
    normal_font = normal_fontbutton.get_font_name()
    headers_font = headers_fontbutton.get_font_name()
    background_color = background_colorbutton.get_color()
    hyperlinks_color = hyperlinks_colorbutton.get_color()
    hover_links_color = hover_links_colorbutton.get_color()
    normal_text_color = normal_text_colorbutton.get_color()
    
    settings.set_str("style/normal_font", normal_font)
    settings.set_str("style/headers_font", headers_font)
    settings.set_gdk_color("style/background_color", background_color)
    settings.set_gdk_color("style/hyperlinks_color", hyperlinks_color)
    settings.set_gdk_color("style/hover_links_color", hover_links_color)
    settings.set_gdk_color("style/normal_text_color", normal_text_color)
    
    html.update_auto_css_file(normal_font, headers_font, background_color, hover_links_color)
    
    dialog.destroy()
    # TODO: is it necessary to explicitly destroy plugin preferences pages?
    
    return response

def on_open_report_clicked(checkbox, report_viewer_entry):
    report_viewer_entry.set_sensitive(checkbox.get_active())

    